<template>
  <div class="decreaseHistogram">
    <!-- <decreaseTable v-if="dealFlow != null" :dealFlow="dealFlow">
    </decreaseTable> -->
    <histogramFund v-if="my_data_list.length > 0" ref="zjlrsj3" :thame="'dark'" :name="'_mydata'" :link="''" :title="title4"
      :listData="my_data_list" :timeData="index_time" :markLine="my_markline" :markPoint="my_markpoint"
      :areaStyle="areaStyle2" :legend1="my_legend" :visualMap="visualMap2"  :width1="'1400px'" :height1="'600px'" value=""
      :ifSetUpMap="true"
      ></histogramFund>
      <button class="income" @click="toFixedToolIndex()">跳转到数据处理</button>
    <decrease ref="decrease"  :day_list="day_data_array" :inflow="inflow_data"
      :dealFlow="dealFlow" :inflowArray="inflowArray" 
       :bug_shell_a_d_a="bug_shell_a_d_a" class="flex-item">
    </decrease>
    <section v-if="diyData==null">
      <div class="input-box  flex-c">
        <el-input class="time" v-model="price_time_input" placeholder="输入买入日期(xxxx-xx-xx)" size="medium"></el-input>
        <el-button @click="gainData(price_time_input)" size="medium">确定 </el-button>
      </div>
      <histogramFund v-if="new_list.length > 0" ref="zjlrsj2" :thame="''" :name="'_zjlrsj2'" :link="''"
        :markPoint="index_markpoint2" :markArea="new_markArea" :title="title4" :listData="new_list" :timeData="index_time"
        :markLine="new_markline" :areaStyle="[null, {}]" :legend1="legend2" :width1="'1400px'" :height1="'600px'"
        :visualMap="new_visualMap" value="">
      </histogramFund>
    </section>

  </div>
</template>
<script>
import histogramFund from "../histogramFund.vue"
import decrease from "./decrease/decrease.vue"
import decreaseTable from "./decrease/decreaseTable.vue"
  import gridData from "./../js/gridData.js"
import dealDecrease from "./decrease/dealDecrease.js"
import decreaceUtil from "./decrease/decreaceUtil.js"
import {
  setMarkLine
} from "../setMarkLine.js"
//获取每个版块买入信息
import {
  dealFlowData
} from "./decrease/dealDecreaseName.js"
import initCaculate from "../js/calculateStopLoss"
import {
  init10TimeData
} from "./decrease/decreasePoint.js"
//处理point
import dealZjrsjPoint from "./js/dealZjrsjPoint.js"
import {
  dealPoint
} from "../common/util.js"
export default {
  data() {
    return {
      inflow_data: [],
      title4: "",
      index_time: [],
      //第二个
      index_markpoint2: [],
      indexfun1: null,
      areaStyle2: [{}],
      visualMap2: null,
      dealFlow: null,
      inflowArray: [],
      calculateStopLoss: null,
      zhidie_array: [],
      legend1: undefined,
      legend2: undefined,
      new_list: [],
      new_markline: [],
      new_markArea: [],
      new_visualMap: [],
      //输入的
      price_time_input: '',
      decrease_data: null,
      //设置
      my_data_list: [],
      my_markline: null,
      my_markpoint: [],
      my_legend: null,
      bug_shell_a_d_a: null,
    }
  },
  created() { },
  props: {
    day_data_array: {
      type: Object,
      default: null,
    },
    diyData: {
      type: Object,
      default: null,
    },
  },
  components: {
    histogramFund,
    decrease,
    decreaseTable
  },
  created() {
    this.initData()
  },
  mounted() {
  },
  watch: {},
  methods: {
    dealLineData(this_time, type2) {
        let that = this
         const calculateStopLoss = initCaculate.calculateStopLoss2(
            [{ klines: this.day_data_array.all_klines }],
            this.day_data_array.all_klines,
            null,
            "",
            this.day_data_array.time
          );
          //处理数据
          let select_data = that.new_list;
          if (select_data.length == 0) return false;
          that.new_list = []
          let max_data = JSON.parse(JSON.stringify(select_data[0].num))
          //缺少最大值= 
          let zdf_num_max = calculateStopLoss.deal_data2.zbs_array[this_time]
          let zdf_num_min = calculateStopLoss.deal_data2.ji_zhidie[this_time]
          // console.log("zdf_num_max==还是需要计算40天的数据", zdf_num_max);
          let gridData2 = gridData.dealMarkLine(
            that.indexfun1.num,
            select_data[0].num[this_time],
            zdf_num_max,
            zdf_num_min
          )
  
          let index_markline = setMarkLine(
            [
              gridData2.top_data5,
              {
                name: "当前的位置",
                num: select_data[0].num[this_time],
                time: that.index_time[this_time]
              },
              gridData2.bottom_data5,
            ],
            that.index_time
          )
          setTimeout(() => {
            that.new_list = select_data
            that.new_markline = index_markline
  
            if (type2 == 2) {
  
            
            }
          }, 1000)
    },
    //获取数据
    gainData(value) {
      let that = this
      let decrease_data = that.decrease_data
      let this_time2 = that.index_time.findIndex((item) => {
        return item === value + ""
      })
      let length1 = this_time2 - 1
      let index_markline = setMarkLine(
        [{
          name: "40天涨幅" + decrease_data[7].num[length1],
          num: parseFloat(that.indexfun1.inflow[length1]) +
            parseFloat(decrease_data[7].num[length1]),
        },],
        that.index_time
      )
      that.new_markArea = [{}, {
        // itemStyle: {
        //   color: 'rgba(255, 0, 0, 0.2)', // 填充颜色（透明红色）
        //   borderColor: 'red', // 边框颜色
        //   borderWidth: 2 // 边框宽度
        // },
        data: [
          [{
            name: '40天涨幅' + decrease_data[7].num[length1],
            xAxis: that.index_time[length1 - 1],
            itemStyle: {
              color: 'rgba(255, 0, 0, 0.3)' // 红色半透明
            },
            emphasis: {
              lineStyle: {
                color: 'green' // markArea 内部折线颜色
              }
            }
          },
          { xAxis: that.index_time[length1 - 39] }
          ],
          [{
              name: that.index_time[length1],
              xAxis: that.index_time[length1 + 10],
              itemStyle: {
                color: 'rgba(0, 255, 0, 0.5)' // 红色半透明
              } },
            {  xAxis: that.index_time[length1 - 1] }
          ]
        ]
      }]
      that.new_visualMap = [
        {
          show: false,
          dimension: 0,
          pieces: [{
            gt: 0,
            lte: length1 - 40,
          },
          {
            gt: length1 - 40,
            lte: length1,
            color: 'red'
          },
          {
            gt: length1,
          }
          ]
        },
      ]
      let thislist = that.new_list;
      that.new_list = []
      setTimeout(() => {
        that.new_markline = [{},
        index_markline[0]
        ]
        that.new_list = thislist
      }, 1000)
    },

    newAccumulative() {
      const that = this;
      const decrease_data = dealDecrease.initData(
        that.indexfun1.num,
        that.day_data_array,
        null,
        that.indexfun1.inflow
      );
      that.decrease_data = decrease_data;
      // 构建 new_list 数据
      const newListItems = [
        { num: that.indexfun1.num, name: that.indexfun1.name },
        { num: that.indexfun1.inflow, name: "累计涨跌幅" },
        
        // { num: that.indexfun1.adjustedGrid, name: '网格间隔' }
      ];
      const indicesToAdd = [5, 6, 9, 10, 7, 8];
      indicesToAdd.forEach(index => {
        newListItems.push({
          num: decrease_data[index].num,
          name: decrease_data[index].name
        });
      });
      that.new_list.push(...newListItems);
      // 调用 dealZjrsjPoint.initData
      const dataForZjrsjPoint = indicesToAdd.map(index => decrease_data[index].num);
      console.log("dataForZjrsjPoint", dataForZjrsjPoint)
      dealZjrsjPoint.initData(dataForZjrsjPoint, this.index_time);
      // 构建 legend2
      const selectedKeys = [
        "this_name",
        "累计涨跌幅",
        "10天内持续跌幅",
        "10天内持续涨幅",
        "20天内持续跌幅",
        "20天内持续涨幅",
        "40天内持续跌幅",
        "40天内持续涨幅",
        //"art数值",
        // "网格间隔"
      ];
      const selectedValues = [false, true, false, false, false, false, false, false, false, false];
      const selectedObj = {};
      selectedKeys.forEach((key, index) => {
        selectedObj[key] = selectedValues[index];
      });
      that.legend2 = { selected: selectedObj };

      // 计算 point_10TimeData
      const point_10TimeData = dealPoint(
        init10TimeData({
          this_num: that.indexfun1.num,
          diefu_data10: decrease_data[6].num,
          time: that.indexfun1.time
        }).zhidie_array.num,
        0
      );
      that.index_markpoint2 = [
        {
          data: point_10TimeData
        }
      ];
    },
    toFixedToolIndex(){
      this.$router.push({path:'/fixedToolIndex'});
    },
    initData() {
      const { day_data_array } = this;
      const that = this;
      that.indexfun1=day_data_array
      // 基础数据初始化
      that.index_time=that.indexfun1.time
      if(this.diyData==null){
        this.newAccumulative();
      }
      that.dealFlowDataFun()
    }, 
    dealFlowDataFun(){
      const that = this;
        //获取买入的价格的位置
      dealFlowData(that.indexfun1.name).then(res => {
        that.my_legend=  {
            selected: {
              累计涨跌幅: false, 盈亏变化: true,
              成本变化: false, 每日输入变化: false, 每日买入卖出: false
            }
          }
        if(this.diyData!=null){
            const dataConfig =this.diyData.list_data;
            dataConfig.forEach(item => that.my_data_list.push(item));
            that.my_markpoint =  [{}, { }];
            let end_num=dealDecrease.analyzeTrend(this.diyData.decrease.day_loss_profit,
            this.diyData.all_time)
            let end_l_a_m=dealDecrease.dealLineAreaMap(
              this.diyData.decrease.day_loss_profit,[end_num[0].num,end_num[1].num]
            )
            that.visualMap2=end_l_a_m.map_data;
            that.my_markline=end_l_a_m.line_data;
            that.bug_shell_a_d_a=this.diyData.decrease;
            that.inflow_data=this.diyData.dnflow_data
            that.dealFlow=this.diyData.dealFlow
          return;
        }
        
        this.dealFlow = res;
        if (!res) return this.$nextTick(() => that.$refs.decrease.toSreach(that.index_time.slice(-1)[0]));
        // 数据组装
        if (res.length) {
        
          // 生成买卖标记点
          decreaceUtil.getFundData(that.indexfun1.name).then(fun_res=>{
            const point2 = res.map(element => {
            const index_time = that.indexfun1.time.findIndex(t => t === element.time);
            return {
              name: "买卖操作",
              value: element.type,
              xAxis: element.time,
              yAxis: fun_res.num_array[index_time],
              itemStyle: { color: element.type.includes('卖出') ? "green" : "red" }
            };
          });
            const funds_data=fun_res.num_array.slice(-that.indexfun1.inflow.length)
            const  deal_bs=decreaceUtil.dealBugShellData(
              {time:fun_res.time_array.slice(-that.indexfun1.inflow.length),
              inflow:funds_data
              }, res);
            that.bug_shell_a_d_a=deal_bs
            sessionStorage.setItem("bug_shell_all_data_array",JSON.stringify(res))
            sessionStorage.setItem("funds_data",JSON.stringify(funds_data))
            sessionStorage.setItem("funds_data_time",JSON.stringify(that.index_time))
            sessionStorage.setItem("funds_data",JSON.stringify(funds_data))

            const dataConfig = [
              { num: funds_data, name: "累计涨跌幅" },
              { num: deal_bs.day_loss_profit, name: "盈亏变化" },
              { num: deal_bs.bug_shell_all_data_array, name: "每日输入变化" },
              { num: deal_bs.shell_cben_array, name: "成本变化" },
              { num: deal_bs.bug_shell_data, name: "每日买入卖出", type_echarts: "bar", itemStyle: 2 },              
              // { num: deal_bs.remaining_profit, name: "盈亏率" }
            ];

            dataConfig.forEach(item => that.my_data_list.push(item));
            that.my_markpoint = 
              [{}, { 
          // data: point2
              }];
              let end_num=dealDecrease.analyzeTrend(deal_bs.day_loss_profit,that.index_time)
              let end_l_a_m=dealDecrease.dealLineAreaMap(
                deal_bs.day_loss_profit,[end_num[0].num,end_num[1].num]
              )
              that.visualMap2=end_l_a_m.map_data;
                //that.areaStyle2=end_l_a_m.area_data;
              that.my_markline=end_l_a_m.line_data;

          
          })
        }
      }).catch(console.error);
    }
  },
 
}
</script>